package com.netrust.quarz.service;

import java.util.List;


import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.netrust.quarz.common.NetrustException;
import com.netrust.quarz.common.enums.OperatorEnums;
import com.netrust.quarz.dao.RealAlarmDao;
import com.netrust.quarz.dao.ServerDao;
import com.netrust.quarz.entity.Server;
import com.netrust.quarz.hw.CommonHttp;
import com.netrust.quarz.hw.token.OcUtils;
import com.netrust.quarz.task.utils.AlarmConverUtils;
import com.netrust.quarz.utils.ProdUtils;

/**
 * 定时生成允许建议所需要的源数据
 * @author visitor
 *
 */
@Component  
public class BulidAlarmRealService{
	
	private static final Logger log = LoggerFactory.getLogger(BulidAlarmRealService.class);

	@Autowired
	OcUtils oc;
	@Autowired
	RealAlarmDao realAlarmDao;
	@Autowired
	ServerDao serverDao;
	
	public List<String> http_run(String op) throws Exception{
		String oe=ProdUtils.op2oe(op).getName();
		
		try {
			CommonHttp ch=new CommonHttp();
			
			oc.setMo(oe);
			List<String> list=ch.getAlarmRealTime(oc);
			
			return list;
		
		} catch (Exception e) {
			throw e;
		}
	}
	
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Transactional(isolation=Isolation.DEFAULT,propagation = Propagation.REQUIRED)
	public void run() throws Exception {
		NetrustException exception=null;
		
		List list=Lists.newArrayList();
		
		try {
			list=this.getAlarmReal(OperatorEnums.Operator_dx_zwww, list);			
		} catch (Exception e) {
			log.error("获取 {} 实时告警数据错误>>>> {}",OperatorEnums.Operator_dx_zwww.getName(),ProdUtils.exception(e));
			exception=new NetrustException("电信政务外网实时告警数据获取异常", e);
		}
		
		try {
			list=this.getAlarmReal(OperatorEnums.Operator_dx_hlw, list);	
		} catch (Exception e) {
			log.error("获取 {} 实时告警数据错误>>>> {}",OperatorEnums.Operator_dx_hlw.getName(),ProdUtils.exception(e));
			exception=new NetrustException("电信互联网实时告警数据获取异常", e);
		}
		
		try {
			list=this.getAlarmReal(OperatorEnums.Operator_yd_zwww, list);	
		} catch (Exception e) {
			log.error("获取 {} 实时告警数据错误>>>> {}",OperatorEnums.Operator_yd_zwww.getName(),ProdUtils.exception(e));
			exception=new NetrustException("移动政务外网实时告警数据获取异常", e);
		}
		
		try {
			list=this.getAlarmReal(OperatorEnums.Operator_yd_hlw, list);	
		} catch (Exception e) {
			log.error("获取 {} 实时告警数据错误>>>> {}",OperatorEnums.Operator_yd_hlw.getName(),ProdUtils.exception(e));
			exception=new NetrustException("移动互联网实时告警数据获取异常", e);
		}
		
		
		if (CollectionUtils.isNotEmpty(list)) {			
			//组装
			List<Server> slist= serverDao.findAll();
			List finalList=new AlarmConverUtils().origin(list,slist);
			realAlarmDao.deleteAll();
			realAlarmDao.save(finalList);
		}
		
		if(exception!=null){
			throw exception;
		}
		
	}
	
	
	private List getAlarmReal(OperatorEnums oe,List list) throws Exception{
		try {
			CommonHttp ch=new CommonHttp();
			oc.setMo(oe.getName());
			
			List<String> strList=ch.getAlarmRealTime(oc);
			
			if(CollectionUtils.isNotEmpty(strList)){
				List list_yd_hlw=ProdUtils.pushList(strList);
				
				if(CollectionUtils.isNotEmpty(list_yd_hlw)){
					list.addAll(list_yd_hlw);
				}				
			}
			
			return list;
		} catch (Exception e) {
			throw new NetrustException(e);
		}
	}

	
	
}
